<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/18
 * Time: 13:09
 */

namespace addons\admin\controller;


class Community extends Common
{
    public function community_list()
    {
        $this->check_login();
        $param = input();
        $cond = [];
        if (isset($param["search"]) && $param["search"]) {
            if (isset($param["province_id"]) && $param["province_id"] && $param["province_id"] != "") {
                $cond["province_id"] = $param["province_id"];
            }
            if (isset($param["city_id"]) && $param["city_id"] != "") {
                $cond["city_id"] = $param["city_id"];
            }
            if (isset($param["county_id"]) && $param["county_id"] && $param["county_id"] != "") {
                $cond["county_id"] = $param["county_id"];
            }
            if (isset($param["community_name"]) && $param["community_name"] && $param["community_name"] != "") {
                $cond["community_name"] = ["LIKE", "%" . $param["community_name"] . "%"];
            }
        }
        $list_ = \addons\admin\service\Community::list_($cond);
        $this->assign("list_", $list_);
        return $this->fetch();
    }

    public function community_edit()
    {
        $this->check_login();
        $id = input("id");
        $del = input("del");
        /** @var \app\common\model\Community $community */
        $community = \app\common\model\Community::get($id);
        if (empty($community)) {
            $community = new \app\common\model\Community();
        } else {
            if ($del) {
                $community->delete();
                $this->success("删除成功");
            }
        }
        /**
         * 执行保存操作
         */
        $province_id = input('province_id');
        $city_id = input('city_id');
        $county_id = input('county_id');
        $community_name = input('community_name');
        $community_address = input('community_address');
        if (input("submit") == 1) {
            $community->province_id = $province_id;
            $community->city_id = $city_id;
            $community->county_id = $county_id;
            $community->community_name = $community_name;
            $community->community_address = $community_address;
            $community->save();
            $this->success("成功");
        }
        $this->assign("data", $community);
        return $this->fetch();
    }
}